<template>
  <div>
    <h1>问题管理</h1>
    <el-button type="primary" @click="openAdd" >添加问题</el-button>
    <el-table
      :data="questionList"
      style="width: 100%">
      <el-table-column
        prop="questionId"
        label="id"
        width="180">
      </el-table-column>
      <el-table-column
        prop="questionName"
        label="问题"
        width="260">
      </el-table-column>
      <el-table-column
        prop="answer"
        label="答案"
        width="260">
      </el-table-column>
      <el-table-column
        prop="qcount"
        label="查询次数"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="180">
        <template slot-scope="scope" >
          <el-button type="primary" @click="openUpdate(scope.row)">修改</el-button>
          <el-button type="primary" @click="delQuestion(scope.row.questionId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="添加/修改问题"
      :visible.sync="dialogVisible"
      :before-close="clearQuestion"
      width="70%">
      <el-form ref="form" :model="question" label-width="80px">
        <el-form-item label="问题">
          <el-input v-model="question.questionName"></el-input>
        </el-form-item>
        <el-form-item label="答案">
          <el-input v-model="question.answer"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addQuestion">执行</el-button>
        </el-form-item>
      </el-form>


    </el-dialog>

  </div>
</template>
<script>
  import {queryQuestion,addQuestion,delQuestion,updateQuestion} from '@/api/question.js'
export default {
  data(){
    return{
      questionList:[],
      searchMap:{},
      dialogVisible:false,
      question:{}
    }
  },
  mounted() {
    this.queryQuestion()
  },
  methods:{
    clearQuestion(){
      this.dialogVisible=false
      this.question={}
    },
    queryQuestion(){
      queryQuestion(this.searchMap).then(res=>{
        if(res.data){
          this.questionList=res.data
        }
      })
    },
    openAdd(){
      this.dialogVisible = true
    },
    addQuestion(){
      addQuestion(this.question).then(res=>{
        alert(res.msg)
        this.question={}
        this.dialogVisible=false
        this.queryQuestion()
      })
    },
    delQuestion(id){
      delQuestion(id).then(res=>{
        alert(res.msg)
        this.queryQuestion()
      })
    },
    openUpdate(row){
      this.dialogVisible=true
      this.question=row
    }

  }
}
</script>
<style>

</style>
